package com.scala.learn.sparksql2

import org.apache.spark.sql.{DataFrame, Dataset, SparkSession}

/**
  * @Copyright: Shanghai Definesys Company.All rights reserved.
  * @Description:
  * @author: chuhaitao
  * @since: 2019/3/9 21:23
  * @history:
  *          1.2019/3/9 created by chuhaitao
  */
object SqlWorldCount {


  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
      .appName("wc")
      .master("local[*]")
      .getOrCreate()

    val lines: Dataset[String] = spark.read.textFile("D:\\tmp\\world.txt")
    // val lines: DataFrame = spark.read.format("text").load("D:\\tmp\\world.txt")
    //导入隐式转换
    import spark.implicits._
    //切分
    val worlds: Dataset[String] = lines.flatMap(_.split(" "))
    val wcDf: DataFrame = worlds.withColumnRenamed("value", "word")
    //创建视图
    wcDf.createTempView("word")
    //写sql
    val res1: DataFrame = spark.sql("select word,count(*) as count from word group by word order by count ")
    //  val frame: DataFrame = spark.sql("select * from word")

    res1.show()
  }

}
